package com.banciyuan.bcywebview.biz.pc.zone.smooth;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;

/**
 * Created by squall on 2016/10/9.
 */

public class PersonTimelineProxyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private RecyclerView.Adapter childAdapter;
    private Context mContext;
    private LayoutInflater layoutInflater;
    private final static int HEADER = 1987;
    private final static int FOOTER = 1988;
    private int min_height;
    private int height;
    private int max = -1;
    private String type;

    public PersonTimelineProxyAdapter(RecyclerView.Adapter childAdapter, Context mContext, String type) {
        this.type = type;
        this.childAdapter = childAdapter;
        this.mContext = mContext;
        this.layoutInflater = LayoutInflater.from(mContext);
        this.min_height = CommonUtils.getHeightPixels((Activity) mContext) - CommonUtils.getStatusPixels((Activity) mContext) - UIUtils.dip2px(93, mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HEADER:
                return new HeaderHolder(layoutInflater.inflate(R.layout.smooth_blank, null));
            case FOOTER:
                return new FooterHolder(layoutInflater.inflate(R.layout.person_smooth_bottom, null));
            default:
                return childAdapter.onCreateViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case HEADER:
                HeaderHolder headerHolder = (HeaderHolder) holder;
                int offset = 0;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    offset = UIUtils.dip2px(20, mContext);
                }
                LinearLayout.LayoutParams paramsheader = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(296, mContext) + offset);
                headerHolder.blank.setLayoutParams(paramsheader);
                break;
            case FOOTER:
                FooterHolder footerHolder = (FooterHolder) holder;
                if (childAdapter.getItemCount() == 1) {
                    if (type.equals("tuijian")) {
                        footerHolder.empty_two.setVisibility(View.VISIBLE);
                    } else {
                        footerHolder.empty_one.setVisibility(View.VISIBLE);
                    }
                    setEmptyHeight(footerHolder, type);
                } else {
                    footerHolder.empty_two.setVisibility(View.GONE);
                    footerHolder.empty_one.setVisibility(View.GONE);
                }
                int footer = min_height - height;
                LinearLayout.LayoutParams paramsfoot = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, footer > 0 ? footer : 1);
                footerHolder.blank.setLayoutParams(paramsfoot);
                break;
            default:
                childAdapter.onBindViewHolder(holder, position - 1);
                setHeight(holder, position - 1);
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADER;
        } else if (position == getItemCount() - 1) {
            return FOOTER;
        } else {
            return childAdapter.getItemViewType(position - 1);
        }
    }

    @Override
    public int getItemCount() {
        return childAdapter.getItemCount() + 2;
    }

    private class HeaderHolder extends RecyclerView.ViewHolder {

        View blank;

        public HeaderHolder(View itemView) {
            super(itemView);
            blank = itemView.findViewById(R.id.blank);
        }
    }

    private class FooterHolder extends RecyclerView.ViewHolder {

        View blank;
        View empty_one;
        View empty_two;

        public FooterHolder(View itemView) {
            super(itemView);
            blank = itemView.findViewById(R.id.blank);
            empty_one = itemView.findViewById(R.id.rl_issue_emptyview);
            empty_two = itemView.findViewById(R.id.rl_recommend_emptyview);
        }
    }

    private void setHeight(RecyclerView.ViewHolder holder, int position) {
        try {
            if (height < min_height && position > max) {
                if (holder.getItemViewType() >= 30 && holder.getItemViewType() <= 36) {
                    holder.itemView.measure(
                            View.MeasureSpec.makeMeasureSpec(CommonUtils.getWidthPixels((Activity) mContext) / 3, View.MeasureSpec.AT_MOST),
                            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                    boolean mod = (position - 1) % 3 == 0;
                    if (mod) {
                        height += holder.itemView.getMeasuredHeight();
                    }
                } else {
                    holder.itemView.measure(
                            View.MeasureSpec.makeMeasureSpec(CommonUtils.getWidthPixels((Activity) mContext), View.MeasureSpec.AT_MOST),
                            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                    height += holder.itemView.getMeasuredHeight();
                }
                max = position;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setEmptyHeight(FooterHolder holder, String type) {
        if (height < min_height) {
            if (!type.equals("tuijian")) {
                holder.empty_one.measure(
                        View.MeasureSpec.makeMeasureSpec(CommonUtils.getWidthPixels((Activity) mContext), View.MeasureSpec.AT_MOST),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                height += holder.empty_one.getMeasuredHeight();
            } else {
                holder.empty_two.measure(
                        View.MeasureSpec.makeMeasureSpec(CommonUtils.getWidthPixels((Activity) mContext), View.MeasureSpec.AT_MOST),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                height += holder.empty_two.getMeasuredHeight();
            }
        }
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void setMax(int max) {
        this.max = max;
    }
}
